package com.example.zhoutao.puzzle.Login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.zhoutao.puzzle.Data.UserData;
import com.example.zhoutao.puzzle.Data.UserDataManager;
import com.example.zhoutao.puzzle.R;

/**
 * @Author ZT on 2019.4
 * @Description 重置密码
 */

public class Resetpwd extends AppCompatActivity {

    private EditText mAccount;
    private EditText mPwd_old;
    private EditText mPwd_new;
    private EditText mPwdCheck;
    private Button mSureBtn;
    private Button mCancelBtn;
    private UserDataManager mUserDataManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpwd);

        mAccount=findViewById(R.id.et_resetpwd_name);
        mPwd_old=findViewById(R.id.et_resetpwd_pwd_old);
        mPwd_new=findViewById(R.id.et_resetpwd_pwd_new);
        mPwdCheck=findViewById(R.id.et_resetpwd_pwdAgain_new);

        mSureBtn=findViewById(R.id.btn_resetpwd_sure);
        mCancelBtn=findViewById(R.id.btn_resetpwd_cancel);

        mSureBtn.setOnClickListener(m_resetpwd_Listener);
        mCancelBtn.setOnClickListener(m_resetpwd_Listener);

        //建立本地数据库
        if (mUserDataManager==null){
            mUserDataManager=new UserDataManager(this);
            mUserDataManager.openDataBase();
        }

    }

    View.OnClickListener m_resetpwd_Listener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btn_resetpwd_sure:
                    resetpwd_check();
                    break;

                case R.id.btn_resetpwd_cancel:
                    Intent intent =new Intent(Resetpwd.this,Login.class);
                    startActivity(intent);
                    finish();
                    break;
            }
        }
    };

    public void resetpwd_check(){
        if (isUserNameAndPwdValid()){
            String userName=mAccount.getText().toString().trim();
            String userPwd_old=mPwd_old.getText().toString().trim();
            String userPwd_new=mPwd_new.getText().toString().trim();
            String userPwdCheck=mPwdCheck.getText().toString().trim();

            int result=mUserDataManager.findUserByNameAndPwd(userName,userPwd_old);

            //返回1说明用户名和密码均正确
            if (result==1){
                if (userPwd_new.equals(userPwdCheck)==false){
                    Toast.makeText(this, "两次新密码输入不一致，请重新输入！", Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    UserData mUser=new UserData(userName,userPwd_new);
                    mUserDataManager.openDataBase();
                    boolean flag=mUserDataManager.updateUserData(mUser);
                    if (flag = false) {
                        Toast.makeText(this, "密码修改失败，请重新尝试！", Toast.LENGTH_SHORT).show();

                    }else{
                        Toast.makeText(this, "密码修改成功！", Toast.LENGTH_SHORT).show();
                        mUser.pwdResetFlag=1;
                        Intent intent=new Intent(Resetpwd.this,Login.class);
                        startActivity(intent);
                        finish();
                    }
                }
            } else if (result == 0) {
                Toast.makeText(this, "密码不正确，请重新输入！", Toast.LENGTH_SHORT).show();
                return;
            }
        }

    }

    public boolean isUserNameAndPwdValid(){
        String userName=mAccount.getText().toString().trim();

        //检查用户是否存在
        int count=mUserDataManager.findUserByName(userName);

        //用户不存在时返回，给出提醒
        if (count<=0){
            Toast.makeText(this, "用户【(tom)】不存在，请重新输入！", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (mAccount.getText().toString().trim().equals("")){
            Toast.makeText(this, "用户名为空，请重新输入！", Toast.LENGTH_SHORT).show();
            return false;
        }else if(mPwd_old.getText().toString().trim().equals("")){
            Toast.makeText(this, "密码为空，请重新输入！", Toast.LENGTH_SHORT).show();
            return false;
        }else if(mPwd_new.getText().toString().trim().equals("")){
            Toast.makeText(this, "新密码为空，请重新输入！", Toast.LENGTH_SHORT).show();
            return false;
        }else if(mPwdCheck.getText().toString().trim().equals("")){
            Toast.makeText(this, "新密码确认为空，请重新输入！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
